package com.higgins.eventtracking.client.health;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.health.components.HealthOverviewChart;
import com.higgins.eventtracking.client.health.components.HealthOverviewChartListBox;
import com.higgins.eventtracking.client.health.overview.AchievementTab;
import com.higgins.eventtracking.client.health.overview.DigestionTab;
import com.higgins.eventtracking.client.health.overview.FrameOfMindTab;
import com.higgins.eventtracking.client.health.overview.GoalsTab;
import com.higgins.eventtracking.client.health.overview.HygieneTab;
import com.higgins.eventtracking.client.health.overview.NegativeTab;
import com.higgins.eventtracking.client.health.overview.HourlyTab;
import com.higgins.eventtracking.client.health.overview.WellBeingTab;

public class Overview extends VerticalPanel	{
	private HealthOverviewChartListBox chartListBox = new HealthOverviewChartListBox();
	private HealthOverviewChart chartPanel = new HealthOverviewChart();
	private AnnotatedChart annotationChart = new AnnotatedChart();
	private TabPanel tabPanel = new TabPanel();
	private DigestionTab digestionTab = new DigestionTab(this);
	private HygieneTab hygieneTab = new HygieneTab(this);
	private FrameOfMindTab frameOfMindTab = new FrameOfMindTab(this);
	private WellBeingTab wellBeingTab = new WellBeingTab(this);
	private AchievementTab achievementsTab = new AchievementTab(this);
	private NegativeTab negativeTab = new NegativeTab(this);
	private boolean firstRun = true;
	private Integer negatives = null;
	private boolean positives = false;
	private Integer achievements = null;
	private boolean goals = false;

	private Boolean tab1 = null;
	private Boolean tab2 = null;
	private Boolean tab3 = null;
	private Boolean tab4 = null;
	private Boolean tab5 = null;
	private Boolean tab6 = null;
	private Boolean tab7 = null;
	
	
	public Overview()	{	}
	
	public void addDigestionTab(Boolean alert)	{
		tab1 = alert;
		addTabs();
	}
	public void addHygieneTab(Boolean alert)	{
		tab2 = alert;
		addTabs();
	}
	public void addWellBeingTab(Boolean alert)	{
		tab3 = alert;
		addTabs();
	}
	public void addFrameOfMindTab(Boolean alert)	{
		tab4 = alert;
		addTabs();
	}
	public void addAchievementsTab(int count)	{
		tab5 = false;
		achievements = count;
		addTabs();
	}
	public void addNegativeTab(Integer negatives)	{
		tab6 = false;
		this.negatives = negatives;
		addTabs();
	}
	public void addGoalsTab()	{
		tab7 = false;
		addTabs();
	}
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		populateTabPanel();
		populateChartPanel();
		firstRun = false;
	}
	public void populateChartPanel()	{
		add(chartListBox);
		chartListBox.addChangeHandler(new ChangeHandler()  {
			@Override
			public void onChange(ChangeEvent event) {
				int selectedIndex = chartListBox.getSelectedIndex();
				String chartName = chartListBox.getItemText(selectedIndex);
				if (chartName.equals("Well Being"))	{
					runWellBeingChart();
				} else if (chartName.equals("Energy")) {
					runEnergyChart();
				} else if (chartName.equals("Bloated"))	{
					runBloatingChart();
				} else if (chartName.equals("Health Points"))	{
					runHealthPointsChart();
				}
			}  });
//		add(chartPanel);
//		chartPanel.executeChart();
		add(annotationChart);
		runWellBeingChart();
	}
	private void runWellBeingChart()	{
		annotationChart.setTitle("Well Being");
		annotationChart.setColumnA("Date");
		annotationChart.setColumnB("Well Being");
		annotationChart.setColumnC(null);
		annotationChart.setScale(10);
		annotationChart.executeChart();
	}
	
	private void runEnergyChart()	{
		annotationChart.setTitle("Energy");
		annotationChart.setColumnA("Date");
		annotationChart.setColumnB("Energy");
		annotationChart.setColumnC(null);
		annotationChart.setScale(10);
		annotationChart.executeChart();
	}
	
	private void runBloatingChart()	{
		annotationChart.setTitle("Bloated");
		annotationChart.setColumnA("Date");
		annotationChart.setColumnB("Bloated");
		annotationChart.setColumnC(null);
		annotationChart.setScale(10);
		annotationChart.executeChart();
	}
	
	private void runHealthPointsChart()	{
		annotationChart.setTitle("Health Points");
		annotationChart.setColumnA("Date");
		annotationChart.setColumnB("Health Points");
		annotationChart.setColumnC(null);
		annotationChart.setScale(200);
		annotationChart.executeChart();
	}
	public void populateTabPanel()	{
		digestionTab.update();
		hygieneTab.update();
		wellBeingTab.update();
		frameOfMindTab.update();
		achievementsTab.update();
		negativeTab.update();
		add(tabPanel);
	}
	private void addTabs()	{
		if (tab1!=null && tab2!=null && tab3!=null && tab4!=null && tab5!=null && tab6!=null)	{
	//		tabPanel.add(digestionTab, "<div class=\"highlightAlertLabel\" style=\"width: 75px;\">Digestion</div>", true);
			tabPanel.add(digestionTab, tab1?"<div class=\"highlightAlertLabel\" style=\"width: 75px;\">Digestion</div>":"Digestion", true);
			tabPanel.add(hygieneTab, tab2?"<div class=\"highlightAlertLabel\" style=\"width: 75px;\">Hygiene</div>":"Hygiene", true);				
			tabPanel.add(wellBeingTab, tab3?"<div class=\"highlightAlertLabel\" style=\"width: 75px;\">Well Being</div>":"Well Being", true);
			tabPanel.add(frameOfMindTab, tab4?"<div class=\"highlightAlertLabel\" style=\"width: 100px;\">FrameOfMind</div>":"FrameOfMind", true);
			tabPanel.add(negativeTab, "Emotional (" + negatives + ")");
			tabPanel.add(achievementsTab, "Achievements (" + achievements + "% )");
			tabPanel.selectTab(0);
			tabPanel.addSelectionHandler(createSelectionHandler());		
		}
	}
	public SelectionHandler<Integer> createSelectionHandler()	{
		SelectionHandler<Integer> selectionHandler = new SelectionHandler<Integer>() {
	        @Override
	        public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
	            if (integerSelectionEvent.getSelectedItem() == 0)	{
	            	digestionTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 1)	{
	            	hygieneTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 2)	{
	            	wellBeingTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 3)	{
	            	frameOfMindTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 4)	{
	            	negativeTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 5)	{
	            	achievementsTab.update();
	            }
	        }
		};
		return selectionHandler;
	}

}
